//
//  CostiInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 25/05/13.
//
//

#import "CostiInterventoViewController.h"
#import "Util.h"

@interface CostiInterventoViewController ()

@end

@implementation CostiInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.array = [NSMutableArray arrayWithCapacity:0];
    [self.array addObject:@"Manodopera"];
    [self.array addObject:@"Componenti"];
    [self.array addObject:@"Accessori"];
    [self.array addObject:@"Importo"];
    [self.array addObject:@"Iva"];
    [self.array addObject:@"Totale"];
    
    [self.table reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)viewDidAppear:(BOOL)animated{
    
}

-(IBAction) indietro{
	[[self navigationController] popViewControllerAnimated:YES];
}


// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Costi Nuovo Intervento";
	return [NSString stringWithFormat: @"Costi Intervento %@",self.intervento.idintervento];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    if([name isEqual:@"Manodopera"] && self.intervento.costo_manodopera != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.costo_manodopera floatValue]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Componenti"] && self.intervento.costo_componenti != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.costo_componenti floatValue]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Accessori"] && self.intervento.costo_accessori != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.costo_accessori floatValue]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Importo"] && self.intervento.importo != nil){
        [cell setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]];
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.importo floatValue]];
    }
    else if([name isEqual:@"Iva"] && self.intervento.iva != nil){
        [cell setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]];
        cell.textLabel.text = [NSString stringWithFormat:@"%@ (21.00%%)",name];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.iva floatValue]];
    }
    else if([name isEqual:@"Totale"] && self.intervento.totale != nil){
        [Util setCellAlert:cell];
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f €",[self.intervento.totale floatValue]];
    }
    
    else{
        cell.textLabel.text = name;
    }
    
    
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.array objectAtIndex:indexPath.row];
    self.nameSelect = name;
    
    if([name isEqual:@"Manodopera"] && self.intervento.costo_manodopera != nil){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Manodopera"
                                         message:@"Costo in euro della manodopera" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        UITextField *alertTextField = [myAlertView textFieldAtIndex:0];
        alertTextField.keyboardType = UIKeyboardTypeDecimalPad;
        if([self.intervento.costo_manodopera floatValue]>0){
            alertTextField.text = [NSString stringWithFormat:@"%.2f",[self.intervento.costo_manodopera floatValue]];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Componenti"] && self.intervento.costo_componenti != nil){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Componenti"
                                                              message:@"Costo in euro di componenti" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        UITextField *alertTextField = [myAlertView textFieldAtIndex:0];
        alertTextField.keyboardType = UIKeyboardTypeDecimalPad;
        if([self.intervento.costo_componenti floatValue]>0){
            alertTextField.text = [NSString stringWithFormat:@"%.2f",[self.intervento.costo_componenti floatValue]];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Accessori"] && self.intervento.costo_accessori != nil){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Accessori"
                                                              message:@"Costo in euro di accessori" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        UITextField *alertTextField = [myAlertView textFieldAtIndex:0];
        alertTextField.keyboardType = UIKeyboardTypeDecimalPad;
        if([self.intervento.costo_accessori floatValue]>0){
            alertTextField.text = [NSString stringWithFormat:@"%.2f",[self.intervento.costo_accessori floatValue]];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Importo"] && self.intervento.importo != nil){
    }
    else if([name isEqual:@"Iva"] && self.intervento.iva != nil){
    }
    else if([name isEqual:@"Totale"] && self.intervento.totale != nil){
    }
    else{
    }
}

-(void) ricalcola{
    double importo = [self.intervento.costo_accessori doubleValue] + [self.intervento.costo_componenti doubleValue] + [self.intervento.costo_manodopera doubleValue];
    double iva = (importo * 0.21);
    self.intervento.importo =[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.2f",importo]];
    self.intervento.iva = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.2f",iva]];
    self.intervento.totale = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.2f",(importo+iva)]];
    [self.table reloadData];
}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1){
        NSString *name = self.nameSelect;
        
        NSString *value = [[alertView textFieldAtIndex:0]text];
        
        if([name isEqual:@"Manodopera"] && self.intervento.costo_manodopera != nil){
            self.intervento.costo_manodopera = [NSDecimalNumber decimalNumberWithString:value];
            [self ricalcola];
        }
        else if([name isEqual:@"Componenti"] && self.intervento.costo_componenti != nil){
            self.intervento.costo_componenti = [NSDecimalNumber decimalNumberWithString:value];
            [self ricalcola];
        }
        else if([name isEqual:@"Accessori"] && self.intervento.costo_accessori != nil){
            self.intervento.costo_accessori = [NSDecimalNumber decimalNumberWithString:value];
            [self ricalcola];
        }
        else if([name isEqual:@"Importo"] && self.intervento.importo != nil){
        }
        else if([name isEqual:@"Iva"] && self.intervento.iva != nil){
        }
        else if([name isEqual:@"Totale"] && self.intervento.totale != nil){
        }
        else{
        }
    }
}




















@end
